﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Entidad;

namespace Persistencia
{
    public class TipoInsumoDAL
    {
        public List<TipoInsumo> TraerTodos(string strNombre)
        {
            SqlConnection cn = Conexion.Conectar();
            try
            {
                SqlCommand cmd = new SqlCommand("spTipoInsumoSel", cn);
                cmd.Parameters.Add(new SqlParameter("@Codigo", null));
                cmd.Parameters.Add(new SqlParameter("@Nombre", strNombre));

                cmd.CommandType = CommandType.StoredProcedure;
                SqlDataAdapter da = new SqlDataAdapter(cmd);

                DataSet ds = new DataSet();
                da.Fill(ds, "TipoInsumo");

                /*Creo la lista*/
                List<TipoInsumo> Lista = (from item in ds.Tables[0].AsEnumerable()
                                              select new TipoInsumo()
                                              {
                                                  Codigo = item.Field<int>("Codigo"),
                                                  Nombre = item.Field<string>("Nombre"),
                                                  Descripcion = item.Field<string>("Descripcion"),

                                              }).ToList();
                return Lista;

            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                cn.Close();
            }
        }

        public int Insertar(TipoInsumo objTipoInsumo)
        {
            SqlConnection cn = Conexion.Conectar();
            try
            {
                SqlCommand cmd = new SqlCommand("spTipoInsumoIns", cn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@Nombre", objTipoInsumo.Nombre));
                cmd.Parameters.Add(new SqlParameter("@Descripcion", objTipoInsumo.Descripcion));

                return cmd.ExecuteNonQuery();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                cn.Close();
            }
        }

        public int Modificar(TipoInsumo objTipoInsumo)
        {
            SqlConnection cn = Conexion.Conectar();
            try
            {
                SqlCommand cmd = new SqlCommand("spTipoInsumoUpd", cn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@Codigo", objTipoInsumo.Codigo));
                cmd.Parameters.Add(new SqlParameter("@Nombre", objTipoInsumo.Nombre));
                cmd.Parameters.Add(new SqlParameter("@Descripcion", objTipoInsumo.Descripcion));

                return cmd.ExecuteNonQuery();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                cn.Close();
            }
        }

        public int Eliminar(int intCodigo)
        {
            SqlConnection cn = Conexion.Conectar();
            try
            {
                SqlCommand cmd = new SqlCommand("spTipoInsumoDel", cn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@Codigo", intCodigo));
                return cmd.ExecuteNonQuery();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                cn.Close();
            }
        }
    }
}
